﻿using System;
using Microsoft.SharePoint.WebPartPages;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace VPLink.WebParts
{
    public class DisplayManagerToolPart : ToolPart
    {
        private HtmlSelect slcDisplayStyle;

        /// <summary>
        /// Constructor for the class.
        /// </summary>        
        public DisplayManagerToolPart()
        {
            this.Title = "Outils de gestion de l'affichage des WebParts :";
            this.PreRender += new EventHandler(DisplayManagerToolPart_PreRender);
        }

        /// <summary>
        /// Create dropdownlist to display lists
        /// </summary>
        override protected void CreateChildControls()
        {
            try
            {
                HtmlTable table = new HtmlTable();
                HtmlTableRow row;
                HtmlTableCell cell;

                HtmlGenericControl divUserDottedLine = new HtmlGenericControl("div");
                divUserDottedLine.Attributes.Add("class", "UserDottedLine");

                row = new HtmlTableRow();

                cell = new HtmlTableCell();
                cell.ColSpan = 2;
                HtmlGenericControl lblDisplayStyle = new HtmlGenericControl("label");
                lblDisplayStyle.InnerText = "Style: ";
                cell.Controls.Add(lblDisplayStyle);
                cell.Controls.Add(new LiteralControl("<br/>"));

                slcDisplayStyle = new HtmlSelect();

                IChangeableDisplayWebPart currentWebPart = ParentToolPane.SelectedWebPart as IChangeableDisplayWebPart;
                if (currentWebPart != null)
                {
                    foreach (string s in currentWebPart.DisplayStyleCollection)
                    {
                        string[] keyValue = s.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                        slcDisplayStyle.Items.Add(new ListItem(keyValue[0], keyValue[1]));
                    }
                    cell.Controls.Add(slcDisplayStyle);
                    cell.Controls.Add(divUserDottedLine);
                }

                row.Cells.Add(cell);

                table.Rows.Add(row);

                Controls.Add(table);
            }
            catch (Exception e)
            {
                Log.Write(e.Message + "\r\n" + e.StackTrace, System.Diagnostics.EventLogEntryType.Error);
            }
        }

        ///	<summary>
        ///	This method is called by the ToolPane object to apply property changes to the selected Web Part. 
        ///	</summary>
        override public void ApplyChanges()
        {
            // Apply property values here.
            IChangeableDisplayWebPart currentWebPart = ParentToolPane.SelectedWebPart as IChangeableDisplayWebPart;
            if (currentWebPart != null)
            {
                currentWebPart.DisplayStyle = int.Parse(slcDisplayStyle.Value);
            }
        }

        ///	<summary>
        ///	Initializes ToolPart values. 
        ///	</summary>
        private void DisplayManagerToolPart_PreRender(object sender, EventArgs ea)
        {
            try
            {
                IChangeableDisplayWebPart currentWebPart = ParentToolPane.SelectedWebPart as IChangeableDisplayWebPart;
                if (currentWebPart != null)
                {
                    foreach (ListItem item in slcDisplayStyle.Items)
                    {
                        item.Selected = (item.Value == currentWebPart.DisplayStyle.ToString());
                    }
                }
            }
            catch (Exception e)
            {
                Log.Write(e.Message + "\r\n" + e.StackTrace, System.Diagnostics.EventLogEntryType.Error);
            }
        }
    }
}